package edu.vt.ece_3574.amber_alert.location;

import java.util.List;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import edu.vt.ece_3574.amber_alert.PrimaryMain;
import edu.vt.ece_3574.amber_alert.R;

/**
 * This class handles displaying the location of the child on a map
 * 
 * @author Alex
 *
 */
public class LocationDisplayerActivity extends MapActivity {
	private MapView mapView;
	
	private MapController mapController;
	
	public static GeoPoint mapPoint;
	
	@Override 
	public void onBackPressed() { 
	   return; 
	} 

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.location_display);
		
		Button returnButton = (Button) findViewById(R.id.backButton);
		returnButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(LocationDisplayerActivity.this, PrimaryMain.class);
				LocationDisplayerActivity.this.finish();
				startActivity(i);
			}
		});
		
		createZoomControls();
		displayLocation();
	}
	
	/**
	 * Creates zoom in and out controls on the map
	 */
	private void createZoomControls() {
		mapView = (MapView) findViewById(R.id.mapView);
		LinearLayout zoomControls = (LinearLayout) findViewById(R.id.zoom);
		@SuppressWarnings("deprecation")
		View zoomControlsView = mapView.getZoomControls();
		
		//Add the zoom controls as a child view of the MapView
		zoomControls.addView(zoomControlsView, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		mapView.displayZoomControls(true);
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	/**
	 * Handles what happens when new location information is received
	 * @param location
	 */
	public void displayLocation() {
		//Only define the MapController if this has not already been done
		if (mapController == null) {
			mapController = mapView.getController();
		}
		
		//Focus on the new point and zoom in
		mapController.animateTo(mapPoint);
		mapController.setZoom(17);
		
		//Add a location marker
        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);   
	}
	
	class MapOverlay extends com.google.android.maps.Overlay
    {
        @Override
        public boolean draw(Canvas canvas, MapView mapView, 
        boolean shadow, long when) 
        {
            super.draw(canvas, mapView, shadow);                   
 
            //---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(mapPoint, screenPts);
 
            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pushpin);            
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         
            return true;
        }
    } 
}
